<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/21
 * Time: 11:31
 */

namespace app\admin\controller;


use app\model\LikeModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Good
{
    public function index()
    {
        // 查找分类数据
        $like = LikeModel::select();

        View::assign('like', $like);
        return View::fetch();
    }
    //修改点赞状态
    public function updtlike()
    {
        // 1. 获取评论id
        // 2. 修改评论状态status = 2就可以了。

        $likeList = Request::param();
        $validate = Validate::rule([
            'user_id|用户id' => 'require|between:1,' . PHP_INT_MAX,
            'article_id|文章id' => 'require|between:1,' . PHP_INT_MAX,
//            'like_status|点赞状态' => 'require|between:1,' . PHP_INT_MAX,

        ]);
        if (!$validate->check($likeList)) {
            echo $validate->getError();
            exit();
        }
        $like = LikeModel::where("user_Id","=",$likeList['user_id'])
            ->where("article_id","=",$likeList['article_id'])
        ->find();
//        var_dump($like->toArray());
        if ($like['like_status']==1){
            $like['like_status']=2;
        }else{
            $like['like_status']=1;
        }
//        var_dump($like['like_status']);
        $like['update_time']=time();
//        $likelist=$like->toArray();
        $result = $like->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ],
        ];
        return json($data);
    }

}